package chapter6.meine.exercises;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;

import basic.lessons.ChessBoardApplet;

public class ChessBoardHighlightOnClick extends JApplet {
	   public static void main(String[] args) {
		      JFrame window = new JFrame("Clickable Checkerboard");
		      ChessBoardPanel content = new ChessBoardPanel();
		      window.setContentPane(content);
		      window.pack(); // Size the window to the preferred size of its content.
		      window.setLocation(100,100);
		      window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		      window.setResizable(false);  // User can't change the window's size.
		      window.setVisible(true);
		   }
	public void init() {
		setContentPane(new ChessBoardPanel());
	}// end of init()
	


	public static class ChessBoardPanel extends JPanel implements MouseListener {
		int rowNum, colNum, rowLast, colLast;
		boolean sameCellClicked;

		public ChessBoardPanel() {
			addMouseListener(this);
			setPreferredSize( new Dimension(160,160) );

		} // end of constructor

		public void paintComponent(Graphics g) {

			// Draw a set of nested black rectangles on a red background.
			// Each nested rectangle is separated by 15 pixels on
			// all sides from the rectangle that encloses it.

			int insetW = 0; // inset by width
			int insetH = 0; // inset by height

			for (int i = 1; i <= 8; i++) {
				for (int j = 1; j <= 8; j++) {
					if ((j + i) % 2 == 0) {
						g.setColor(Color.black);
						g.fillRect(insetW, insetH, 20, 20);
						insetH += 20;
					} else {
						g.setColor(Color.red);
						g.fillRect(insetW, insetH, 20, 20);
						insetH += 20;
					}
				}
				insetW += 20;
				insetH = 0;
			}// end of for()

			highlightCell(g, rowNum, colNum);

		}// end of paintComponent ()

		public void highlightCell(Graphics g, int rowNum, int colNum) {
			if (sameCellClicked == false) {
				g.setColor(Color.cyan);
				g.drawRect((rowNum - 1) * 20, (colNum - 1) * 20, 19, 19);
				g.drawRect((rowNum - 1) * 20+1, (colNum - 1) * 20+1, 17, 17);
			} else if ((rowNum + colNum) % 2 == 0) {
				g.setColor(Color.black);
				g.fillRect((rowNum - 1) * 20, (colNum - 1) * 20, 20, 20);
			} else {
				g.setColor(Color.red);
				g.fillRect((rowNum - 1) * 20, (colNum - 1) * 20, 20, 20);
			}
		}// end of highlightCell()

		@Override
		public void mouseClicked(MouseEvent e) {
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}

		@Override
		public void mousePressed(MouseEvent e) {
			int x = e.getX();
			int y = e.getY();
			if (x > 160 || y > 160) // process clicks only within 160x160 range
				return;			
			rowNum = x / 20 + 1;
			colNum = y / 20 + 1;
			
			if (rowNum == rowLast && colNum == colLast)
				sameCellClicked = true;
		
			else 
				sameCellClicked = false;
			repaint();

		}// end of mousePressed

		@Override
		public void mouseReleased(MouseEvent e) {
			int x = e.getX();
			int y = e.getY();
			rowLast = x / 20 + 1;
			colLast = y / 20 + 1;
			if (sameCellClicked == true){
				rowLast = 0;
				colLast = 0;
			}
		}// end of mouseReleased()

	}// end of class ChessBoardPanel
}// end of class RollingTwoDicesApplet

